<?php

/**
 * Created by EPP.
 * User: yuxun
 * Date: 18/6/26
 * Time: 21:21
 */
class PlayerController extends AdminBaseController {
    
    public function lists(){
    	$this->setTitle('玩家信息列表');
    	
    	$filter = new PubFilter();
    	$grid   = new PubGrid();
    	
    	$onlineOptions = array(
    			''  => '全部',
    			'1' => '在线',
    	);
    	
    	$filter->addOssServer()
    	->addFilter('role_name', 'input', '角色名')
    	->addFilter('role_id', 'input', '角色ID')
    	->addFilter('account_name', 'input', '平台账号')
    	->addFilter('is_online', 'radio', ' ', array('options' => $onlineOptions));
    	
    	$params = $filter->get();
    	
    	if(PubFunc::isAjax()){
    		$data = PlayerService::indexData($params);
    		$this->out = array(
    				'count'     => $result['count'],
    				'data'      => $data,
    				'result'    => true
    		);
    		$this->outType = 'json';
    	}else{
    		$header = PlayerService::indexHeader();
    	
    		$url = PubFunc::ajaxUrl('/oss/?r=player/lists', $params);
    	
    		$grid->addGrid($header, $url, array(
    				'length' => 10
    		));
    	
    		$this->out['filter'] = $filter->show();
    		$this->out['grid'] = $grid->show();
    	}
    }
    
    
    public function view(){
    	$this->setTitle('玩家信息');
    	
    	$roleId = PubRequest::R('roleId');
    	$game = PubBase::getCurrentGame();
    	
    	$this->out['roleId'] = $roleId;
    	$this->out['game'] = $game;
    	
    }
    
    public function search(){
    	$this->setTitle('玩家搜索');
    	$game = PubBase::getCurrentGame();
    	$this->out['game'] = $game;
    	
//     	$platform = PubBase::getCurrentPlatform();
//     	$game = PubBase::getCurrentGame();
//     	$serverList = ServerService::getOptions($platform, $game);
//     	if(empty($server)){
//     		$server = key($serverList);
//     	}
//     	$this->out['serverList'] = $serverList;
    	
    	$specialList = SpecialServerService::specialList();
    	$this->out['specialList'] = $specialList;
    	
    	$modServer = new ModServer();
    	$keyList = array_keys($specialList);
    	$list = $modServer->getServerForSpecialId($game,$keyList[0]);
    	$options = array();
    	foreach ($list as $key=>$val){
    		$options[$val['id']] = $val['server_name'].' ('.$val['server_num'].')';
    	}
    	$this->out['serverList'] = $options;
    	
    	$tabList = PlayerService::playTab();
    	$this->out['tabList'] = $tabList;
    }
    
    public function searchAttr(){
    	$this->setTitle('玩家属性搜索');
    	$game = PubBase::getCurrentGame();
    	$this->out['game'] = $game;
    	 
//     	$platform = PubBase::getCurrentPlatform();
//     	$game = PubBase::getCurrentGame();
//     	$serverList = ServerService::getOptions($platform, $game);
//     	if(empty($server)){
//     		$server = key($serverList);
//     	}
//     	$this->out['serverList'] = $serverList;

    	$specialList = SpecialServerService::specialList();
    	$this->out['specialList'] = $specialList;
    	 
    	$modServer = new ModServer();
    	$keyList = array_keys($specialList);
    	$list = $modServer->getServerForSpecialId($game,$keyList[0]);
    	$options = array();
    	foreach ($list as $key=>$val){
    		$options[$val['id']] = $val['server_name'].' ('.$val['server_num'].')';
    	}
    	$this->out['serverList'] = $options;
    	 
    	$tabList = array('5'=>'玩家属性');
    	$this->out['tabList'] = $tabList;
    }
    
    
    public function search_info(){
    	$roleId = PubRequest::R('roleId');
    	$server = PubRequest::R('server');
    	$type = PubRequest::R('type');
    	$good_type = PubRequest::R('good_type');
    	$skill_type = PubRequest::R('skill_type');
    	$s_type = PubRequest::R('s_type');
    	if($s_type == 1){  //查询角色名称
    		$roleId = $roleId;
    	}
    	if($s_type == 2){  //查询角色id
    		//先根据角色id查询出角色名称
    		$roleId = PlayerService::getRoleName($roleId);
    	}

    	$this->layout = '';
    	$tpl = PlayerService::typeTpl();
    	$this->tpl = $tpl[$type];
    	$this->out['roleId'] = $roleId;
    	 
    	$params = array(
    			'uid'    => $roleId,
    			'search_type'    => $type,
    	);
    	if($type == 12){
    		//先从基本信息查到帮派id
    		$api = new ApiService($server);
    		$result_1 = $api->roleInfo(array('uid'=>$roleId,'search_type'=>1));
    	    $data_array_1 = json_decode($result_1['data'],true);
    		$guild_uid = $data_array_1[0]['guild_uid'];
    		$params['guild_uid'] = $guild_uid;
    	}
    	$api = new ApiService($server);
    	$result = $api->roleInfo($params);    	    	
    	
    	$data_array = json_decode($result['data'],true);
    	$data = $data_array[0];
    	
    	if(isset($good_type)){
    		$data = PlayerService::searchGoodsInfo($good_type,$data);
    	}else if(isset($skill_type)){
    		$data = PlayerService::searchSkillInfo($skill_type,$data);
    	}else{
    		$data = PlayerService::searchInfo($type,$data);
    	}
    	
    	if($type == 3 || isset($good_type)){
    		$goodsTypeList = PlayerService::goodsTypeList();
    		$this->out['goodsTypeList'] = $goodsTypeList;
    		$this->out['good_type'] = $good_type;
    	}
    	
    	if($type == 4 || isset($skill_type)){
    		$skillTypeList = PlayerService::skillTypeList();
    		$this->out['skillTypeList'] = $skillTypeList;
    		$this->out['skill_type'] = $skill_type;
    	}
    	
    	//$result['state'] = true;
    	//$result['data'] = $data;    	
    	$this->out['role'] = $result;
    	$this->out['data'] = $data;
    	
//     	echo "<pre>";
//     	var_dump($result);
//     	echo "</pre>";
    	
    }
    
    public function autoComplete(){
    	$val = PubRequest::R('val');
    	$server_id = PubRequest::R('server'); 
    	$s_type =  PubRequest::R('s_type'); 

    	$info = ServerService::get($server_id);
    	if(!$info){
    		$out = array('status'=>0,'msg'=>'服务器不存在','data'=>$result);   		
    	}else{
    		$result = PlayerService::autoComplete($val,$info['develop_id'],$s_type);   		
    		$out = array('status'=>1,'msg'=>'查询成功','data'=>$result);
    	}
    	$this->out = $out;
    	$this->outType = 'json';
    	
    }
    
    public function autoOpenId(){
    	$val = PubRequest::R('val');
    	$result = PlayerService::autoOpenId($val);
    	$out = array('status'=>1,'msg'=>'查询成功','data'=>$result);
    	$this->out = $out;
    	$this->outType = 'json';
    }
    
    public function getRoleForOpenId(){
    	$openid = PubRequest::R('openid');
    	$result = PlayerService::getRoleForOpenId($openid);
    	$out = array('status'=>1,'msg'=>'查询成功','data'=>$result);
    	$this->out = $out;
    	$this->outType = 'json';
    }
    
    
    public function setMarket(){
    	$server = PubRequest::R('server');
    	$status = PubRequest::R('op_type');
    	$uid = PubRequest::R('market_uid');
    	
    	$params = array(
    			'status'    => $status,
    			'uid'    => $uid,
    	);

    	$api = new ApiService($server);
    	$result = $api->setMarketStatus($params);
    	
    	$this->out = $result;
    	$this->outType = 'json';
    }
    
    /**
     * 账号封禁
     */
    public function banUser(){
    	if(PubFunc::isAjax()){
    		$ar = PubRequest::AR();
    		$out = PlayerService::banUser($ar);
    		$this->out = $out;
    		$this->outType = 'json';
    	}else{
    		$this->layout = '';
    	}
    }
    
    /**
     * 角色禁言
     */
    public function shutupUser(){
    	if(PubFunc::isAjax()){
    		$ar = PubRequest::AR();
    		$out = PlayerService::shutupUser($ar);
    		$this->out = $out;
    		$this->outType = 'json';
    	}else{
    		$this->out['channelType']   = ShutupUserService::channelType();
    		$this->layout = '';
    	}
    }
    
    /**
     * 以下方法废弃
     */
    
    
    /**
     * 玩家基本信息
     */
    public function basic_info(){
    	$roleId = PubRequest::R('roleId');
    	$this->layout = '';
    	$this->tpl = 'basic_info';
    	$this->out['roleId'] = $roleId; 	
    }
    
    /**
     * 玩家准备
     */
    public function equipment(){
    	$roleId = PubRequest::R('roleId');
    	$this->layout = '';
    	$this->tpl = 'equipment';
    	$this->out['roleId'] = $roleId;
    }
    
    /**
     * 背包物品
     */
    public function goods(){
    	$roleId = PubRequest::R('roleId');
    	$this->layout = '';
    	$this->tpl = 'goods';
    	$this->out['roleId'] = $roleId;
    }
    
    /**
     * 玩家技能
     */
    public function skill(){
    	$roleId = PubRequest::R('roleId');
    	$this->layout = '';
    	$this->tpl = 'skill';
    	$this->out['roleId'] = $roleId;
    }
    
    /**
     * 玩家属性
     */
    public function attribute(){
    	$roleId = PubRequest::R('roleId');
    	$this->layout = '';
    	$this->tpl = 'attribute';
    	$this->out['roleId'] = $roleId;
    }
    
    /**
     * 符文
     */
    public function rune(){
    	$roleId = PubRequest::R('roleId');
    	$this->layout = '';
    	$this->tpl = 'rune';
    	$this->out['roleId'] = $roleId;
    }
    
    /**
     * 聚魂
     */
    public function soul(){
    	$roleId = PubRequest::R('roleId');
    	$this->layout = '';
    	$this->tpl = 'soul';
    	$this->out['roleId'] = $roleId;
    }
    
    
    /**
     * 称号
     */
    public function title(){
    	$roleId = PubRequest::R('roleId');
    	$this->layout = '';
    	$this->tpl = 'title';
    	$this->out['roleId'] = $roleId;
    }
    
    /**
     * 时装
     */
    public function fashion(){
    	$roleId = PubRequest::R('roleId');
    	$this->layout = '';
    	$this->tpl = 'fashion';
    	$this->out['roleId'] = $roleId;
    }
    
    /**
     * 剑魂
     */
    public function sword(){
    	$roleId = PubRequest::R('roleId');
    	$this->layout = '';
    	$this->tpl = 'sword';
    	$this->out['roleId'] = $roleId;
    }
    
    /**
     * 幻形
     */
    public function metamorphosis(){
    	$roleId = PubRequest::R('roleId');
    	$this->layout = '';
    	$this->tpl = 'metamorphosis';
    	$this->out['roleId'] = $roleId;
    }
    
    /**
     * 成就
     */
    public function achievement(){
    	$roleId = PubRequest::R('roleId');
    	$this->layout = '';
    	$this->tpl = 'achievement';
    	$this->out['roleId'] = $roleId;
    }
    
    /**
     * 帮派
     */
    public function faction(){
    	$roleId = PubRequest::R('roleId');
    	$this->layout = '';
    	$this->tpl = 'faction';
    	$this->out['roleId'] = $roleId;
    }
    
    

}